﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace Imgur.Net.Auth
{
    /// <summary>
    /// Authentication context which provides modification of web requests for a registered Imgur user.
    /// </summary>
    public class RegisteredUserAuthContext : ImgurAuthContext
    {
        /// <summary>
        /// Constructs an instance of this AuthContext.
        /// </summary>
        /// <param name="clientId">The ClientID token as assigned by Imgur to all registered applications.</param>
        public RegisteredUserAuthContext(string clientId, RegistrationTokenContext tokenContext)
        {
            if ( string.IsNullOrEmpty(clientId ) )
                throw new ArgumentNullException("clientId");

            if ( tokenContext == null)
                throw new ArgumentNullException("tokenContext");


            ClientId = clientId;
            RegistrationContext = tokenContext;
        }

        /// <summary>
        /// Current Client ID.
        /// </summary>
        public string ClientId { get; private set; }

        /// <summary>
        /// Current context type. Anonymous in this case.
        /// </summary>
        public AuthContextType ContextType { get { return AuthContextType.Anonymous; } }

        /// <summary>
        /// Modify the given web request to add authentication tokens for Imgur. After processing the
        /// web request will contain a valid anonymous authentication token valid to Imgur.
        /// </summary>
        /// <param name="req">The request to modify.</param>
        public void AddContextToRequest(ref WebRequest request)
        {
            if ( request == null)
                throw new ArgumentNullException("request");

            string bearerToken = string.Format("Bearer {0}", RegistrationContext.AccessToken);
            request.Headers.Add("Authorization", bearerToken);
        }


        public RegistrationTokenContext RegistrationContext { get; private set; }
    }
}
